<template>
  <el-row :gutter="4" style="height: 100%">
    <el-col :span="5" style="height: 100%">
      <div style="height: 100%; overflow: auto">
        <el-tree
          class="tree"
          :data="flowTree"
          :props="{ children: 'sons', label: 'label' }"
          :default-expand-all="true"
          :expand-on-click-node="false"
          @node-click="treeClick"
          highlight-current
        ></el-tree>
      </div>
    </el-col>
    <el-col :span="19">
      <div>
        <div class="header">
          <span style="margin-left: 20px">表单搜索</span>
          <el-input
            placeholder="请输入关键字搜索表单"
            v-model="req.keyword"
            style="width: 200px; margin-left: 10px"
          ></el-input>
          <el-button
            type="primary"
            style="margin-left: 34px"
            size="small"
            @click="getMySharedForms()"
          >
            搜索
          </el-button>
        </div>
        <div class="main">
          <el-table
            style="overflow: auto; height: 100%"
            :data="res.Items"
            stripe
            border
            highlight-current-row
          >
            <el-table-column label="流程" width="320px">
              <template slot-scope="scope">
                <div style="margin-bottom: 5px">
                  <el-tag type="danger" v-if="!scope.row.FSH_IsRead">
                    未读
                  </el-tag>
                  <el-tag type="primary" v-if="scope.row.WFM_Status == 1">
                    {{ scope.row.WFM_StatusDesc }}[{{ scope.row.FRS_Name }}]
                  </el-tag>
                  <el-tag type="success" v-else-if="scope.row.WFM_Status == 2">
                    {{ scope.row.WFM_StatusDesc }}
                  </el-tag>
                  <el-tag type="danger" v-else>
                    {{ scope.row.WFM_StatusDesc }}
                  </el-tag>
                </div>
                <span style="color: blue">{{ scope.row.FromUserName }}</span>
                于
                <span style="color: blue">
                  {{ scope.row.FSH_CreateTime | formatDate }}
                </span>
                分享给您的
                <span style="color: blue">{{ scope.row.FLOW_Name }}</span>
                <div v-if="scope.row.FSH_Comment">
                  留言：{{ scope.row.FSH_Comment }}
                </div>
                <!-- <el-link type="primary">
                    状态：{{ scope.row.FRS_Name }}
                  </el-link> -->
              </template>
            </el-table-column>
            <el-table-column label="发起人及日期" width="140px">
              <template slot-scope="scope">
                <div>{{ scope.row.USR_Name }}</div>
                {{ scope.row.WFM_CreateTime | formatDate }}
              </template>
            </el-table-column>
            <el-table-column label="表单" width="300px">
              <template slot-scope="scope">
                <span>{{ scope.row.WFM_FormDataName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="表单信息">
              <template slot-scope="scope">
                <div v-if="scope.row.WFM_FormDataCode">
                  搜寻码：{{ scope.row.WFM_FormDataCode }}
                </div>
                <div v-for="item in scope.row.FieldValues" :key="item.FFV_ID">
                  {{ item.FF_DisplayName }}: {{ item.FFV_DisplayValue }}
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="属性1" width="250px">
                <template slot-scope="scope">
                  {{ scope.row.WFM_Prop1Name }}: {{ scope.row.WFM_Prop1 }}
                </template>
              </el-table-column>
              <el-table-column label="属性2" width="250px">
                <template slot-scope="scope">
                  {{ scope.row.WFM_Prop2Name }}: {{ scope.row.WFM_Prop2 }}
                </template>
              </el-table-column>
              <el-table-column label="属性3" width="250px">
                <template slot-scope="scope">
                  {{ scope.row.WFM_Prop3Name }}: {{ scope.row.WFM_Prop3 }}
                </template>
              </el-table-column>
              <el-table-column label="属性4" width="250px">
                <template slot-scope="scope">
                  {{ scope.row.WFM_Prop4Name }}: {{ scope.row.WFM_Prop4 }}
                </template>
              </el-table-column> -->
            <el-table-column fixed="right" label="操作" width="100px">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-search"
                  @click="toFormPage(scope.row.WFM_ID)"
                >
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="width: 100%; height: 40px">
          <el-pagination
            layout="total,prev,pager,next"
            style="float: right"
            background
            small
            :current-page.sync="req.pageIndex"
            :total="res.Total"
            :page-size="req.pageSize"
            :page-sizes="[10, 15, 20]"
            @size-change="
              (val) => {
                req.pageSize = val
                getMySharedForms()
              }
            "
            @current-change="getMySharedForms()"
          ></el-pagination>
        </div>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import {
  getCompanyFlowTree,
  getMySharedForms,
} from '@/api/flowCenter/form/formManage'
export default {
  data: function () {
    return {
      curNode: null,
      flowTree: [],
      req: {
        keyword: null,
        bsnId: null,
        flowId: null,
        pageIndex: 1,
        pageSize: 10,
      },
      res: {
        Items: [],
        Total: 0,
      },
    }
  },
  methods: {
    treeClick(item) {
      this.curNode = item
      this.getMySharedForms()
    },
    getMySharedForms() {
      if (this.curNode) {
        switch (this.curNode.level) {
          case 0: {
            this.req.bsnId = null
            this.req.flowId = null
            break
          }
          case 1: {
            this.req.bsnId = this.curNode.id
            this.req.flowId = null
            break
          }
          case 2: {
            this.req.bsnId = null
            this.req.flowId = this.curNode.id
            break
          }
        }
      }
      getMySharedForms(this.req).then(({ data }) => {
        this.res = data
      })
    },
    getCompanyFlowTree() {
      getCompanyFlowTree().then(({ data }) => {
        this.flowTree = data
        this.getMySharedForms()
      })
    },
    toFormPage(formId) {
      this.$router.push({
        name: 'formInfoPage',
        query: { formId },
      })
    },
  },
  mounted() {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
    this.getCompanyFlowTree()
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>

<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
.main {
  height: calc(75vh - 60px);
  overflow: auto;
}
</style>
